package com.bbzn.device.client.service.impl.cwting;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bbzn.device.client.constant.YG;
import com.bbzn.device.client.dto.ct.request.RequestCreateCommand;
import com.bbzn.device.client.service.emq.OrderSendService;
import com.ctg.ag.sdk.biz.AepDeviceCommandClient;
import com.ctg.ag.sdk.biz.aep_device_command.CreateCommandRequest;
import com.ctg.ag.sdk.biz.aep_device_command.CreateCommandResponse;
import org.springframework.stereotype.Component;

/**
 * @Author wulongbo
 * @Date 2021/9/11 11:09
 * @Version 1.0
 */
@Component
public class UnifySendServiceImpl extends OrderSendBaseServiceImpl implements OrderSendService {

    @Override
    public CreateCommandResponse createCommand(String imei, String masterKey, RequestCreateCommand requestCreateCommand, String content) throws Exception {
        if (content.isEmpty()) {
            return null;
        }
        // 统一指令下发接口
        requestCreateCommand.setContent(JSONObject.parseObject(content));
        // 1.bean对象转换成byte数组
        byte[] body = JSON.toJSONString(requestCreateCommand).getBytes();
        AepDeviceCommandClient client = AepDeviceCommandClient.newClient().appKey(YG.APP_KEY)
                .appSecret(YG.APP_SECRET)
                .build();
        CreateCommandRequest request = new CreateCommandRequest();
        // set your request params here
        request.setParamMasterKey(masterKey);    // single value
        request.setBody(body);    //具体格式见前面请求body说明

        CreateCommandResponse createCommandResponse = client.CreateCommand(request);
        System.out.println(createCommandResponse);
        client.shutdown();
        return createCommandResponse;
    }
}
